from framework.base_page import BasePage
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from framework.public_class import logger


class LoginPage(BasePage):

    # 移动鼠标悬停在登录头像上
    def mouse_on_face(self):
        longin_above = self.find_element("class=>face")
        # 执行移动操作
        try:
            ActionChains(self.driver).move_to_element(longin_above).perform()  # 鼠标悬停在登录头像上
        except Exception as e:
            logger.error("鼠标悬停在登录头像上: %s" % e)
            self.get_windows_img(error_info="鼠标悬停头像")  # take screenshot

    # 点击头像上的登录按钮
    def click_login_btn_on_face(self):
        self.element_click("id=>J_layerlogin")

    # 点击登录弹窗上的登录按钮
    def click_longin_btn_on_login_frame(self):
        self.element_click("id=>J_login")

    # 切换frame到登录弹窗中
    def switch_frame_to_login(self):
        self.switch_frame(selector="xpath=>//*[@id='layui-layer1']/div/div/iframe")

    # 点击退出按钮
    def click_logout_btn(self):
        self.element_click("id=>J_logout")

    # 检测登录成功的toast是否显示出来（toast太快，检测不出来，此方法暂时没有用到）
    def wait_login_toast(self):
        text = "登录成功,正在跳转..."
        locator = (By.XPATH, "//div[@class='layer-custom-text']")
        return self.toast_is_visible(locator=locator, text=text, timeout=10)
